#include <stdio.h>
#include <stdlib.h>
#include <sys/queue.h>

struct SListItem
{
    int data;
    SLIST_ENTRY(SListItem) entry;
};
/*
 struct SListItem
 {
    int data;
    struct {
        struct SListItem* sle_next;
    } entry;
 }
 */
void slist_demo()
{
    struct SListItem* item = NULL;
    SLIST_HEAD(SListHead, SListItem) shead;
    /*
     struct SListHead {
         struct SListItem* slh_first;
     } shead;
     */
    SLIST_INIT(&shead);

    item = (struct SListItem*)malloc(sizeof(struct SListItem));
    item->data = 1;

    SLIST_INSERT_HEAD(&shead, item, entry);
    /*
     item->entry.sle_next = (&shead)->slh_first;
     (&shead)->slh_first = item;
     */

    item = (struct SListItem*)malloc(sizeof(struct SListItem));
    item->data = 2;

    SLIST_INSERT_HEAD(&shead, item, entry);
    /*
     item->entry.sle_next = (&shead)->slh_first;
     (&shead)->slh_first = item;
     */

    SLIST_FOREACH(item, &shead, entry){
        printf("%d ", item->data);
    }
    /*
     for(item = (&shead)->slh_first; item; item = item->entry.sle_next){
        ...
     }
     */
    printf("\n");

    while(!SLIST_EMPTY(&shead)){
        item = SLIST_FIRST(&shead);
        printf("remove %d\n", item->data);
        SLIST_REMOVE(&shead, item, SListItem, entry);
        free(item);
    }
    /*
     while(!((&shead)->slh_first == NULL)){
         item = (&shead)->slh_first;
         ...
         (&shead)->slh_first = (&shead)->slh_first->entry.sle_next;
         ...
     }
     */
}

int main(void)
{
	slist_demo();
}

